package win.zxshuo.www.schedule;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import win.zxshuo.www.model.BookModel;
import win.zxshuo.www.service.ApplicationContextProvider;
import win.zxshuo.www.service.BookService;
import win.zxshuo.www.spider.BaiduSearch;
import win.zxshuo.www.spider.JieqiSpider;
import win.zxshuo.www.spider.QidianSpider;
import win.zxshuo.www.utils.SystemUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class SchedulerMethods {
    Log logger = LogFactory.getLog(getClass());

    @Resource
    BaiduSearch baiduSearch;

    @Resource
    JieqiSpider jieqiSpider;

    @Resource
    QidianSpider qidianSpider;

    @Autowired
    BookService bookService;



    public void spiderQidianRanks(){
        try {
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/yuepiao?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/hotsales?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/newvipclick?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/click?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/recom?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/collect?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/vipup?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/vipcollect?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/vipreward?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/signnewbook?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/pubnewbook?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/newsign?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/newauthor?style=1&page");
            logger.info("采集起点排行榜结束");
            //TODO 将起点女生的排行采集做好之后将会加入
        }catch (Exception e){
            logger.info("定时采集起点排行出错，请老板查看日志");
            logger.error(e.getCause());
        }
    }


    // 获取每本书的更新
    public void spideBooksForUpdate(){
        // 获取所有更新状态为更新中的书籍
        List<List<BookModel>> listOfBooklist = new ArrayList<>();
        List<BookModel> bookModelList  = bookService.getAllBookNeedsToUpdate();
        if (bookModelList.size()>1000){
            listOfBooklist = bookService.separateBookListBy(1000,bookModelList);
        }else {
            listOfBooklist.add(bookModelList);
        }

        logger.info("获取的书籍数量"+listOfBooklist.size());

        // 获取内核数决定是否启用多线程(暂时不根据内核数来决定而是根据书籍多少来决定使用多少个线程)
        Integer coreNum = SystemUtils.getCoreNum();
        if (coreNum>=5){
            for (List<BookModel> bookModels:listOfBooklist){
                ThreadUpdateBooks threadUpdateBooks = ApplicationContextProvider.getBean("tUpdateBooks",ThreadUpdateBooks.class);
                threadUpdateBooks.setBookModels(bookModels);
                threadUpdateBooks.start();
            }

        }

        //TODO 更新书籍线程

    }









}
